/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.entity.User;
import com.wii.elpro.entity.Quotation;
import com.wii.elpro.entity.Spdcf;
import com.wii.elpro.dao.QuotationDAOInterface;
import com.wii.elpro.dao.hb.InqueryDAOImpl;
import com.wii.elpro.dao.hb.QuotationDAOImpl;
import com.wii.elpro.dao.hb.ScatteredFunction;
import com.wii.elpro.dao.hb.SpdcfDAOImpl;
import com.wii.elpro.entity.Inquery;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class DefaultAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");
        ActionMessages messages = new ActionMessages();
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String status = (String)sess.getAttribute("statusMenu");
        //QuotationDAOInterface daoQ = new QuotationDAOImpl();
        try {
            if(status.equals("ADMN")){                
                return mapping.findForward("menuAdmin");
            }
            if(status.equals("MRKT")){
                List<Spdcf> spdcf = new SpdcfDAOImpl().getSpdcf();
                List<Quotation> quotations = new QuotationDAOImpl().getQuotations();
                List<Inquery> inqueries = new InqueryDAOImpl().getInqueries();
                
                List<Spdcf> s = ScatteredFunction.getReadySpdcf(spdcf, quotations);
                List<Inquery> i = ScatteredFunction.getNewInquiry(inqueries, spdcf);
                if(s.size()!=0) {
                    messages.add("customMessage", new ActionMessage("customMessage", "You have "+s.size()+" SPDCF(s) to processed."));
//                    System.out.println("------------>"+spdcf.size());
                }
                
                if(i.size()!=0) {
                    messages.add("customMessage", new ActionMessage("customMessage", "You have "+i.size()+" Inquiry(s) to processed."));
//                    System.out.println("------------>"+spdcf.size());
                }
                
                
            }
//            forward = "success";
        } catch (Exception ex) {
            ex.printStackTrace();
//            forward = "fail";
        }
        saveMessages(sess, messages);
        return mapping.findForward("success");
    }
}
